new Vue({
    el: '#app',
    data: {
        //购物车数组
        shopListArr: [],
        /*是否全选*/
        isSelectAll: false,
        /*商品总价*/
        totalPrice: 0,
        //是否隐藏删除面板
        isHideJump: false,
        //当前要删除的商品
        currenDelShop: {},

    },
    //组件加载完毕，请求数组。业务处理
    mounted() {
        this.getLocalData()
    },
    methods: {
        //1.请求数据
        getLocalData() {
            this.$http.get('data.json').then(response => {

                const res = response.body;
                if (res) {
                    this.shopListArr = res.allShops.shopList;
                }

            }, response => {
                alert('err，请求数据失败')
            })
        },
        //选中所有商品
        selctAll(flag) {
            //总控制
            this.isSelectAll = !flag;
            //遍历所有商品数据
            this.shopListArr.forEach((value, index) => {
                if (typeof value.checked === 'undefined') {
                    this.$set(value, 'checked', !flag)
                } else {
                    value.checked = !flag
                }
            })
            this.getAllPrice()
        },
        //单个商品
        singerShopSelect(shop) {
            /*判断有无属性*/
            if (typeof shop.checked === 'undefined') {
                this.$set(shop, 'checked', true)
            } else {
                shop.checked = !shop.checked
            }
            /*检查是否满足全选	*/
            this.hasSelectAll()
            this.getAllPrice()
        },
        /*是否满足全选*/
        hasSelectAll() {
            let flag = true;
            this.shopListArr.forEach((value, index) => {
                if (!value.checked) {
                    flag = false;
                }
            })
            this.isSelectAll = flag
        },
        //计算总价格
        getAllPrice() {
            let totalPrice = 0;
            this.shopListArr.forEach((value, index) => {
                //如果被选中
                if (value.checked) {
                    totalPrice += value.shopPrice * value.shopNum
                }
            })
            this.totalPrice = totalPrice
        },
        //单个商品加减
        subShop(shop, flag) {
            if (flag) { //加
                shop.shopNum += 1
            } else {
                if (shop.shopNum <= 1) {
                    shop.shopNum = 1
                    return
                }
                shop.shopNum -= 1
            }
            //重新计算总价
            this.getAllPrice()
        },
        //删除商品
        deletShop(shop) {
            this.isHideJump = true
            this.currenDelShop = shop
        },
        //确定点击删除
        deletShopOK() {
            this.isHideJump = false
            const index = this.shopListArr.indexOf(this.currenDelShop)
            this.shopListArr.splice(index, 1)
                //重新计算价格
            this.getAllPrice()
        },
        total_go() {
            alert('肯定没有结算接口三')
        }
    }

})